#!/usr/bin/env python
# coding:utf-8

from __future__ import with_statement
import sys
import cPickle as pickle
from pprint import pprint

yinbiao = u"""aāáǎàiīíǐìoōóǒòuūúǔùeēéěè"""

def yinbiao_filter(y):
    pos = yinbiao.find(y)
    if pos > -1:
        y = yinbiao[pos/5*5]
    return y

def main(path):
    dic = {}
    n = 0
    with open(path) as f:
        for i in f.readlines():
            line = i.strip().split("\t")
            if line[1] in ("kHanyuPinyin", "kXHC1983"):
                uni = unichr(int(line[0][2:], 16))
                pinyin = line[2].decode("utf-8").split(" ")
                mao_pos = i.find(":")
                d = [i[i.find(":")+1:].split(",") if i.find(":") > -1 else i.split(",") for i in pinyin] #[1].decode("utf-8")
                pinyin = []
                [pinyin.extend(i) for i in d]
                #if " " in line[2]:
                #    print pinyin
                #    print line[2]
                #    print d
                #    break
                #pinyin = "".join([yinbiao_filter(i) for i in pinyin]) #过滤音标
                #pinyin = pinyin.split(",")
                pinyin = ["".join(map(yinbiao_filter, i)) for i in pinyin]
                pinyin = list(set(pinyin))
                dic[uni] = pinyin
                n = n + 1

    pprint(len(dic))
    with open("pinyin.db", "w") as f:
        pickle.dump(dic, f)

if __name__ == "__main__":
    main(*sys.argv[1:])
